﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Geometry
{
    /// <summary>
    /// This class represents a plane polyline in a two dimensional space.
    /// </summary>
    [Serializable]
    public class Polyline2D : GeometricObject, IPolyline2D
    {
        /// <summary>
        /// The point data of the polyline.
        /// </summary>
        private List<Point2D> lineData;

        /// <summary>
        /// Initializes a new instance of the <see cref="Polyline2D"/> class.
        /// </summary>
        public Polyline2D()
        {
            this.lineData = new List<Point2D>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Polyline2D"/> class.
        /// </summary>
        /// <param name="polyline">The polyline to clone.</param>
        public Polyline2D(Polyline2D polyline)
        {
            if (polyline == (Polyline2D) null)
            {
                throw new ArgumentNullException("polyline");
            }

            this.lineData = polyline.LineData;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Polyline2D"/> class.
        /// </summary>
        /// <param name="linePoints">An array of line points which should be added to the polyline.</param>
        public Polyline2D(Point2D[] linePoints)
        {
            if (linePoints == (Point2D[]) null)
            {
                throw new ArgumentNullException("linePoints");
            }

            this.lineData = new List<Point2D>();

            for (int i = 0; i < linePoints.Length; i++)
            {
                this.lineData.Add(linePoints[i]);
            }
        }

        /// <summary>
        /// Gets or sets the line data of the polyline.
        /// </summary>
        /// <value>The line data of the polyline.</value>
        public List<Point2D> LineData
        {
            get { return lineData; }
            set { lineData = value; }
        }

        /// <summary>
        /// Checks if the polyline has a closed or open geometry.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this polyline is closed; otherwise, <c>false</c>.
        /// </returns>
        public bool IsClosed()
        {
            if (this.lineData.Count > 2)
            {
                return (this.lineData[0] == this.lineData[this.lineData.Count - 1]);
            }

            return false;
        }

        /// <summary>
        /// Adds the point to the polyline.
        /// </summary>
        /// <param name="point">The point which should be added to the polyline.</param>
        public void AddPoint(Point2D point)
        {
            this.lineData.Add(point);
        }

        /// <summary>
        /// Gets a point of the polyline at a specified position.
        /// </summary>
        /// <param name="index">The position of the point in the datalist.</param>
        /// <returns>A point of the polyline at a specified position.</returns>
        public Point2D GetPoint(int index)
        {
            return this.lineData[index];
        }

        /// <summary>
        /// Gets the number of line points.
        /// </summary>
        /// <returns>The number of line points.</returns>
        public int GetNumberOfPoints()
        {
            return this.lineData.Count;
        }

        /// <summary>
        /// Deletes the point which is placed at a specified position.
        /// </summary>
        /// <param name="index">The specified index position.</param>
        public void DeletePoint(int index)
        {
            this.lineData.RemoveAt(index);
        }

        /// <summary>
        /// Deletes all points of the polyline which are even to the compareparameter.
        /// </summary>
        /// <param name="point">The point to compare for removing points from the polyline.</param>
        public void DeletePoint(Point2D point)
        {
            int i = 0;

            while (i < this.lineData.Count)
            {
                if (this.lineData[i] == point)
                {
                    this.lineData.RemoveAt(i);
                    continue;
                }

                i++;
            }
        }

        /// <summary>
        /// Scales every point in the polyline to a specified factor.
        /// </summary>
        /// <param name="scaleFactor">The scale factor.</param>
        public void ScalePolyLine(double scaleFactor)
        {
            foreach (Point2D p in this.lineData)
            {
                p.ScaleTransformation(scaleFactor);
            }
        }

        /// <summary>
        /// Scales every point in the polyline to a specified factor.
        /// </summary>
        /// <param name="scaleFactorX">The scale factor for the x element.</param>
        /// <param name="scaleFactorY">The scale factor for the y element.</param>
        public void ScalePolyLine(double scaleFactorX, double scaleFactorY)
        {
            foreach (Point2D p in this.lineData)
            {
                p.ScaleTransformation(scaleFactorX, scaleFactorY);
            }
        }

        /// <summary>
        /// Translates the polyline with a specified value.
        /// </summary>
        /// <param name="translateValue">The translate value.</param>
        public void TranslatePolyLine(double translateValue)
        {
            foreach (Point2D p in this.lineData)
            {
                p.TranslateTransformation(translateValue);
            }
        }

        /// <summary>
        /// Translates the polyline with a specified value.
        /// </summary>
        /// <param name="translateValueX">The translate value for the x element.</param>
        /// <param name="translateValueY">The translate value for the y element.</param>
        public void TranslatePolyLine(double translateValueX, double translateValueY)
        {
            foreach (Point2D p in this.lineData)
            {
                p.TranslateTransformation(translateValueX, translateValueY);
            }
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first polyline.</param>
        /// <param name="b">The second polyline.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Polyline2D a, Polyline2D b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            if (a.GetNumberOfPoints() != b.GetNumberOfPoints())
            {
                return false;
            }

            for (int i = 0; i < a.GetNumberOfPoints(); i++)
            {
                if (a.GetPoint(i) != b.GetPoint(i))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first polyline.</param>
        /// <param name="b">The second polyline.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Polyline2D a, Polyline2D b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Compares the current instance of a polyline with another polyline object. 
        /// </summary>
        /// <param name="compare">The line to compare.</param>
        /// <returns>True if the position of the polylines are even otherwise the method will 
        /// return false.</returns>
        public bool CompareTo(Polyline2D compare)
        {
            return this == compare;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            String result = "";

            foreach (Point2D p in this.lineData)
            {
                result += p.ToString() + "|";
            }

            return result;
        }
    }
}